﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class ICAlOutDetail : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        //Socket cliSocket = null;
        string ipAddr = "192.168.1.211";
        int port = 1024;
        private string ID;

        private string FBrNo;

        public ICAlOutDetail()
        {
            InitializeComponent();
        }

        public ICAlOutDetail(string fno ,string _ID,out int num)
        {           
            InitializeComponent();
            FBrNo = fno;
            num =0;
            DataSet ds = bjs.GetICADetailOut(FBrNo);
            if (ds.Tables[0].Rows.Count > 0)
            {
                num = 1;
            }
            this.ID = _ID;
        }

        private void ICAlOutDetail_Load(object sender, EventArgs e)
        {
            if (FBrNo == null || FBrNo == "")
            {
            }
            else
            {
                DataGridStyle();
                BinData();
            }
        }

        //获取数据
        private void BinData()
        {
            DataSet ds = bjs.GetICADetailOut(FBrNo);
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            dataGrid1.DataSource = dt;
            this.tb_PkgsBarCode.Text = "";
            this.tb_FEntryNo.Text = "";
            this.tb_ItemSku.Text = "";
            this.tb_PlaceName.Text = "";
        }

        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "ICAlDetailOut";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "流水号";
            cl1.MappingName = "FEntryNo";
            cl1.Width = 0;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "包装码";
            cl2.MappingName = "PkgsBarCode";
            cl2.Width = 80;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
            cl5.HeaderText = "助记码";
            cl5.MappingName = "HelperCode";
            cl5.Width = 45;
            dts.GridColumnStyles.Add(cl5);    
             DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
             cl6.HeaderText = "调出仓库";
             cl6.MappingName = "FName";
             cl6.Width = 60;
             dts.GridColumnStyles.Add(cl6);              

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "库位名称";
            cl4.MappingName = "PlaceName";
            cl4.Width = 100;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "SKU";
            cl3.MappingName = "ItemSku";
            cl3.Width = 160;
            dts.GridColumnStyles.Add(cl3);

          

            dataGrid1.TableStyles.Add(dts);

        }

        private void tb_PkgsBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            bool lb = false;
            string FEntryNo = "";
            string ItemSku = "";
            string PlaceName = "";

            if (e.KeyCode == Keys.Enter)
            {
                string PkgsBarCode = this.tb_PkgsBarCode.Text;
                if (PkgsBarCode == "" || PkgsBarCode == null)
                {
                    MessageBox.Show("请扫描包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else
                {
                    lb = bjs.GetICAOut(FBrNo, PkgsBarCode, out FEntryNo, out ItemSku, out PlaceName);
                    if (lb)
                    {
                        this.tb_FEntryNo.Text = FEntryNo;
                        this.tb_ItemSku.Text = ItemSku;
                        this.tb_PlaceName.Text = PlaceName;
                        this.button1.Focus();
                    }
                    else
                    {
                        MessageBox.Show("扫描包装码在本调拨单中不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        this.tb_PkgsBarCode.Text = "";
                        return;
                    }

                }

            }

        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index); 
            }
            //string FEntryNo = ((DataTable)(dataGrid1.DataSource)).Rows[index][0].ToString();
            //string PkgsBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index][1].ToString();
            //string PlaceBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index][2].ToString();
            //string PlaceName = ((DataTable)(dataGrid1.DataSource)).Rows[index][3].ToString();

            //this.tb_FEntryNo.Text = FEntryNo.Trim();
            //this.tb_PkgsBarCode.Text = PkgsBarCode;
            //this.tb_PlaceBarCode.Text = PlaceBarCode;
            //this.tb_PlaceName.Text = PlaceName;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool lb = false;
            string FEntryNo = this.tb_FEntryNo.Text.Trim();
            string ItemSku = this.tb_ItemSku.Text.Trim();
            string PkgsBarCode = this.tb_PkgsBarCode.Text.Trim();

            if (FEntryNo == "" || FEntryNo == null) 
            {
                MessageBox.Show("请扫描包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            else 
            {
                lb = bjs.UpdateICADetail("",PkgsBarCode,"", "","", "3");
                if (lb)
                {
                    this.tb_PkgsBarCode.Focus();
                    DataGridStyle();
                    BinData();
                }
            }           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ICAlOut ica = new ICAlOut(this.ID);
            ica.Show();
            this.Hide();
        }
    }
}